package com.niknik.woody.rest.rule;

import com.niknik.woody.rest.MainRoomRes;
import com.niknik.woody.rest.guice.RestContainerFactory;
import com.niknik.woody.utils.wunit.guice.InjectableTest;
import com.niknik.woody.utils.wunit.log.WLog;
import com.niknik.woody.utils.wunit.log.WLogger;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.test.framework.AppDescriptor;
import com.sun.jersey.test.framework.JerseyTest;
import com.sun.jersey.test.framework.WebAppDescriptor;
import org.junit.rules.ExternalResource;

/**
 * User: yoda
 * Date: 08.12.11
 * Time: 8:13
 */

public class RestClient
    extends ExternalResource {

  private static final WLogger log = WLog.get(RestClient.class);

  private JerseyTest jerseyRes = new JerseyTest() {

    @Override
    protected AppDescriptor configure() {

      setTestContainerFactory(new RestContainerFactory(InjectableTest.getInjector()));
      return new WebAppDescriptor.Builder(MainRoomRes.class.getPackage().getName()).build();
    }
  };

  @Override
  protected void before()
      throws Throwable {
    super.before();
    jerseyRes.setUp();
  }

  @Override
  protected void after() {
    super.after();
    try {
      jerseyRes.tearDown();
    }
    catch (Exception ex) {
      log.fatalExCatch(ex, "Failed to tear down " + JerseyTest.class.getName());
    }
  }

  public WebResource resource() {
    return jerseyRes.resource();
  }
}
